package com.tdzh.maimaiti.view;


import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.*;
import android.widget.ImageButton;
import android.widget.Toast;

import com.tdzh.maimaiti.R;
import com.tdzh.maimaiti.bean.RequestContent;

public class MainActivity extends AppCompatActivity implements BaseFragment.OnFragmentInteractionListener,OnClickListener{

    private IndexFragment indexFragment;
    private AskFragment askFragment;
    private LookFragment lookFragment;
    private MyPageFragment myPageFragment;
    private HomeFragment homeFragment;
    private DetailsOfRequestFragment detailsOfRequestFragment;
    private MyFriendsFragment myFriendsFragment;
    //private RequestContent requestContent;

    private final static String ITOA = "indexTOask";
    private final static String ATOL = "askTOlook";
    private final static String LTOM = "lookTOmy";
    private final static String MTOI = "myTOindex";

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageButton indexbutton = (ImageButton) findViewById(R.id.button1);
        ImageButton askbutton = (ImageButton) findViewById(R.id.button2);
        ImageButton lookbutton = (ImageButton) findViewById(R.id.button3);
        ImageButton mypagebutton = (ImageButton) findViewById(R.id.button4);

        indexbutton.setOnClickListener(this);
        askbutton.setOnClickListener(this);
        lookbutton.setOnClickListener(this);
        mypagebutton.setOnClickListener(this);
        setDefaultFragment();
    }


    private void setDefaultFragment()
    {
        FragmentManager fm = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
        /*indexFragment = new IndexFragment();
        ft.replace(R.id.maincontent, indexFragment).commit();*/
        homeFragment = new HomeFragment();
        ft.replace(R.id.maincontent, homeFragment).commit();
        // transaction.commit();
    }

    @Override
    public void onClick(View v)
    {
        FragmentManager fm = getSupportFragmentManager();
        // 开启Fragment事务
        android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();

        switch (v.getId())
        {
            case R.id.button1:
                if (homeFragment == null)
                {
                    // SharedPreferences requestCache = getSharedPreferences("requestCache", Context.MODE_PRIVATE);

                    homeFragment = new HomeFragment();
                }
                // 使用当前Fragment的布局替代id_content的控件
                ft.replace(R.id.maincontent, homeFragment);

                break;
            case R.id.button2:
                if (askFragment == null)
                {
                    askFragment = new AskFragment();
                }
                // 使用当前Fragment的布局替代id_content的控件
                ft.replace(R.id.maincontent, askFragment);
                break;
            case R.id.button3:
                if (lookFragment == null)
                {
                    lookFragment = new LookFragment();
                }
                // 使用当前Fragment的布局替代id_content的控件
                ft.replace(R.id.maincontent, lookFragment);
                break;
            case R.id.button4:
                if (myPageFragment == null)
                {
                    myPageFragment = new MyPageFragment();
                }
                // 使用当前Fragment的布局替代id_content的控件
                ft.replace(R.id.maincontent, myPageFragment);
                break;
        }
        // transaction.addToBackStack();

        // transaction.commit();
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }


    @Override
    public void onFragmentInteraction(Bundle bundle, View v) {
        FragmentManager fm = getSupportFragmentManager();
        // 开启Fragment事务
        android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
        String fNum = bundle.getString("fNum");
        switch (fNum){
            case "A":

                homeFragment = new HomeFragment();
                ft.replace(R.id.maincontent, homeFragment);
                // System.out.println("back to indexFragment" + RequestContent.ITEMS.size());
                break;
            case "H":
                //System.out.println("www" + v.getId());
                Toast.makeText(this, bundle.getInt("itemPos") + "click", Toast.LENGTH_SHORT).show();
                RequestContent.RequestItem requestItem = (RequestContent.RequestItem) bundle.getSerializable("requestItem");
              //  if(requestItem == null){
                //   Toast.makeText(this, "该问题可能已经被删除", Toast.LENGTH_SHORT).show();
               // }
                detailsOfRequestFragment = new DetailsOfRequestFragment(requestItem);

               // System.out.println("yhhhhhhhhhhhhhhhhh");
                ft.replace(R.id.maincontent, detailsOfRequestFragment);

                break;
            case "M":
                if(bundle.getString("bNum").equals("4")){
                    myFriendsFragment = new MyFriendsFragment();
                    ft.replace(R.id.maincontent, myFriendsFragment);
                }
                if(bundle.getString("bNum").equals("1")){
                    Intent intent = new Intent(MainActivity.this, MyInterestActivity.class);
                    startActivity(intent);

                }
        }
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();

    }

    // {{再按一次退出
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            if ((System.currentTimeMillis() - exitTime) > 2000)
            {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else
            {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    // }}
}
